package com.qike.duola.scheduler;

import com.qike.duola.domain.DuolaPlan;
import com.qike.duola.domain.SysUserWithBLOBs;
import com.qike.duola.dto.MsgAddDto;
import com.qike.duola.enums.MessageTypeEnums;
import com.qike.duola.enums.OriginTypeEnums;
import com.qike.duola.mapper.DuolaPlanMapper;
import com.qike.duola.mapper.SysUserMapper;
import com.qike.duola.service.DuolaMessageService;
import com.qike.duola.service.DuolaPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Author: Eric.zhou
 * @Date: 2020/11/23 11:32
 */
@Component
public class SchedulerTask {
    @Autowired
    private DuolaPlanService duolaPlanService;

    @Autowired
    private DuolaMessageService duolaMessageService;

    @Autowired
    private SysUserMapper sysUserMapper;


    private static final SimpleDateFormat dateFormat =
            new SimpleDateFormat("HH:mm:ss");

    /**
     * 每分钟执行一次判断当前是否有超过30分钟还未发货的计划 如果有就通知下发人
     */
    @Scheduled(fixedRate = 36000)
    private void process(){
        //找出还未发货并超过30分钟的生产计划
       List<DuolaPlan> limitPlans =  duolaPlanService.findLimit();
       if (limitPlans.size()>0){
           for (DuolaPlan limitPlan : limitPlans) {
               //todo 发送消息
               SysUserWithBLOBs sysUserWithBLOBs = sysUserMapper.selectByPrimaryKey(limitPlan.getUpdateUid());
               MsgAddDto msgAddDto = new MsgAddDto();
               msgAddDto.setTitle(limitPlan.getProjectName()+"还未发货,请联系生产人员");
               msgAddDto.setMsgType(MessageTypeEnums.system.getEcode());
               msgAddDto.setOriginId(limitPlan.getId());
               msgAddDto.setSendUid(sysUserWithBLOBs.getId());
               msgAddDto.setOriginType(OriginTypeEnums.SYSTEM_NOTICE.getEcode());
               try {
                   duolaMessageService.msgAdd(msgAddDto);
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }

       }

//        System.out.println("now time is " + dateFormat.format(new Date()));
    }

}
